package com.situ.mall.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class HandlerInterceptor1 implements HandlerInterceptor {
	/**
	 * 第一次访问时候调用构造方法创建HandlerInterceptor对象，
	 * 后面访问只访问里面的方法
	 */
	public HandlerInterceptor1() {
		System.out.println("HandlerInterceptor1.HandlerInterceptor1()");
	}
	
   /**
    * 执行Controller方法前会执行这个方法，
    * 返回true表示继续执行，返回false表示终止执行
    * 用户权限验证，登录验证都可以写到这里
    */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object chain) throws Exception {
		System.out.println("HandlerInterceptor1.preHandle()");
		//获取请求的url
	    String url=request.getRequestURI();
	    //判断url是否是公开地址（实际使用时要将公开地址配置到文件中）
	    //这里公开地址是登录提交的地址
	    if(url.indexOf("login.action")>=0){
	        //如果进行登录提交，放行
	        return true;
	    }
	    //判断session
	    HttpSession session=request.getSession();
	    String username= (String)session.getAttribute("UserName");
	    if(username!=null){
	        //身份存在，放行
	        return true;
	    }
	    
	    //放行指定的
	    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		String uri = httpServletRequest.getRequestURI();
		String servletPath = httpServletRequest.getServletPath();
		System.out.println("uri : " + uri);
		System.out.println("servletPath" + servletPath);
		
		int lastIndex = servletPath.lastIndexOf(".");
		String extension = "";
		if (lastIndex != -1) {
			extension = servletPath.substring(lastIndex);
		}
	    if ("/user/login.jsp".equals(servletPath)
				|| "/user/getRegisterPage.action".equals(servletPath)
				|| "/user/checkName.action".equals(servletPath)
				|| "/user/add.action".equals(servletPath)
				|| "/user/loginout.action".equals(servletPath)
				|| "/user/YZM.action".equals(servletPath)
				|| "/checkImg".equals(servletPath)
				|| "/login".equals(servletPath)
				|| ".js".equals(extension)
				|| ".css".equals(extension)
				|| ".png".equals(extension)) {
				return true;
	    }
	    
	    //执行到这里，表示用户身份需要认证，跳转登录页面
        request.getRequestDispatcher("/WEB-INF/back/user/login.jsp").forward(request, response);
        
        //return false表示拦截，不向下执行
        //return true表示放行
        return true;
	}
	
   /**
    * 执行Controller方法之后会执行这个方法，
    * 但是是在返回ModelAndView之前
    */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("HandlerInterceptor1.postHandle()");
	}

   /**
    * 执行Controller方法之后并且ModelAndView视图已经返回时候执行这个方法
    * 即彻底执行完时调用
    */
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("HandlerInterceptor1.afterCompletion()");
		
	}
	
}
